package com.hc.markbook.window;

import com.hc.markbook.data.DataCenter;
import com.hc.markbook.procesor.DefaultSourceNoteData;
import com.hc.markbook.procesor.MDFreeMarkerProcessor;
import com.hc.markbook.procesor.Processor;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.Notifications;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class NoteListWindow {
    private JTextField tfTopic;
    private JTable tbContent;
    private JButton btnCreate;
    private JButton btnClear;
    private JButton btnClose;
    private JPanel contentPanel;

    private void init() {
        tbContent.setModel(DataCenter.TABLE_MODEL);
        tbContent.setEnabled(true);
    }

    public NoteListWindow(Project project, ToolWindow toolWindow) {
        init();
        tfTopic.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                // 输入框 回车 生成文档
                if (10 == e.getKeyCode()) {
                    generateDoc(project);
                }
            }
        });
        btnCreate.addActionListener(e -> generateDoc(project));
        btnClear.addActionListener(e -> DataCenter.reset());
        btnClose.addActionListener(e -> toolWindow.hide(null));
    }

    /**
     * 生成文档
     */
    private void generateDoc(Project project) {
        String topic = tfTopic.getText();
        String fileName = topic + ".md";
        if (topic == null || "".equals(topic)) {
            MessageDialogBuilder.yesNo("操作结果", "文档标题没有输入");
            return;
        }
        VirtualFile virtualFile = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(),
                project, project.getBaseDir());
        // 选了直接关闭
        if (virtualFile == null) {
            return;
        }
        String path = virtualFile.getPath();
        String fileFullPath = path + "/" + fileName;

        Processor processor = new MDFreeMarkerProcessor();
        try {
            processor.process(new DefaultSourceNoteData(fileFullPath, topic, DataCenter.NOTE_LIST));
            NotificationGroup notificationGroup = new NotificationGroup("markbook_id",
                    NotificationDisplayType.BALLOON, true);
            Notification notification = notificationGroup.createNotification("生成文档成功：" + fileFullPath, MessageType.INFO);
            Notifications.Bus.notify(notification);
        } catch (Exception ex) {
            System.out.println("error-------------");
            ex.printStackTrace();
        }
    }

    public JPanel getContentPanel() {
        return contentPanel;
    }
}
